package com.lujianfei.powersearch.ui.preview.viewmodel

import android.net.Uri
import android.provider.MediaStore
import android.view.View
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableField
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.lujianfei.powersearch.utils.SnapshotUtils
import com.lujianfei.powersearch.utils.SystemUtils
import kotlinx.coroutines.*

class FullScreenPreviewViewModel: ViewModel() {

    val imageList = ObservableArrayList<String>()

    val totalSizeDisplay = ObservableField("0")

    var currentIndex = ObservableField(0)

    val currentPageSelected = ObservableField("0")

    val showLoading = ObservableField(false)

    var currentPageIdx = 0

    val onPageSelected:(position:Int,realPosition:Int)->Unit = { position,realPosition->
        currentPageIdx = realPosition
        currentPageSelected.set("${position + 1}")
        showIndicator.set(true)
        delayGone()
    }

    var onVideoClickCallback:((String)->Unit) ?= null
    val onViewTapListener: ((View) -> Unit) = {
        checkIndicatorVisible()
        if (imageList[currentPageIdx].contains("video")) {
            onVideoClickCallback?.invoke(imageList[currentPageIdx])
        }
    }

    var onLongClickListenerCallback:((String)->Unit) ?= null
    val onLongClickListener:((String)->Unit) = {
        onLongClickListenerCallback?.invoke(it)
    }

    // 顶部动画相关参数
    var startY = 0f
    var endY = 0f

    val showIndicator = ObservableBoolean(true)

    var jobDelayGone:Job ?= null

    fun loadData(images: String?, currentIndex: Int = 0) {
        imageList.clear()
        images?.let { images->
            val imageArr = images.split(",")
            if (imageArr.size == 1) {
                imageList.add(images) // Single image
                this.currentIndex.set(currentIndex)
                this.totalSizeDisplay.set("1")
            } else {
                this.currentIndex.set(currentIndex + 1)
                imageList.add(imageArr.last())
                imageArr.forEach {
                    imageList.add(it) // Multiple images
                }
                imageList.add(imageArr.first())
                this.totalSizeDisplay.set("${imageList.size - 2}")
            }
        }
        delayGone()
    }

    private fun delayGone() {
        jobDelayGone?.cancel()
        jobDelayGone = viewModelScope.launch {
            delay(2000)
            showIndicator.set(false)
        }
    }

    private fun checkIndicatorVisible() {
        if (!showIndicator.get()) {
            showIndicator.set(true)
            delayGone()
        } else {
            showIndicator.set(false)
        }
    }

    fun savePhoto(uriString: String, finishCallback:()->Unit) {
        viewModelScope.launch(Dispatchers.Main) {
            showLoading.set(true)
            withContext(Dispatchers.IO) {
                kotlin.runCatching {
                    val uri = Uri.parse(uriString)
                    val bitmap = MediaStore.Images.Media.getBitmap(SystemUtils.getApplicationContext().contentResolver, uri)
                    SnapshotUtils.saveBitmap2Gallery(fileName = "${System.currentTimeMillis()}.jpg", bitmap = bitmap)
                }
                true
            }
            showLoading.set(false)
            finishCallback.invoke()
        }
    }
}